package com.android.hq.ganktoutiao.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.hq.ganktoutiao.R;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

/**
 * Created by Administrator on 2017/5/15.
 */
public class ZhuCe extends AppCompatActivity {


    Button fanhui;
    TextView number;
    TextView yanzheng;
    TextView fasong;
    TextView password1;
    TextView password2;
    Button zhuce;
    Handler handler;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_item_zhuce);

        fanhui = (Button) findViewById(R.id.button_fanhui);
        number = (TextView) findViewById(R.id.edit_shoujihaoma);
        yanzheng = (TextView) findViewById(R.id.edit_yanzhengma);
        fasong = (TextView) findViewById(R.id.button_huoquyanzhengma);
        password1 = (TextView) findViewById(R.id.edit_shezhimima);
        password2 = (TextView) findViewById(R.id.edit_quemima);
        zhuce = (Button) findViewById(R.id.button_querenzhuce);

        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ZhuCe.this, ZhuCeActivity.class);
                startActivity(intent);
                finish();
            }
        });

        fasong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numberP = number.getText().toString();

                if (numberP.equals(null) || numberP.equals("")) {
                    //1.上下文  2.显示的字符串  3.显示的时间长短。Toast默认的有两个LENGTH_LONG(长3.5秒)和LENGTH_SHORT(短2秒)
                    Toast.makeText(ZhuCe.this, "手机号码不能为空", Toast.LENGTH_LONG).show();
                } else {

                    //  将数据发送到服务器，并对数据进行检查，返回相应的值进行接下来的操作
                    handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);

                            if (msg.what == 1) {

                                Toast.makeText(ZhuCe.this, "验证码发送成功", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(ZhuCe.this, "号码错误", Toast.LENGTH_LONG).show();

                            }
                        }
                    };
                    duanxin(numberP);
                }
            }
        });

        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numberP = number.getText().toString();
                String yanzhengM = yanzheng.getText().toString();
                String passwordO = password1.getText().toString();
                String passwordT = password2.getText().toString();

                if ((numberP.equals(null) || numberP.equals("")) || (yanzhengM.equals(null) || yanzhengM.equals(""))) {
                    Toast.makeText(ZhuCe.this, "号码以及验证码不能为空", Toast.LENGTH_LONG).show();
                } else if ((!numberP.equals(null) || !numberP.equals("")) || (!yanzhengM.equals(null) || !yanzhengM.equals(""))) {

                    if ((passwordO.equals(null) || passwordO.equals("")) || (passwordT.equals(null) || passwordT.equals(""))) {
                        Toast.makeText(ZhuCe.this, "密码不能为空", Toast.LENGTH_LONG).show();
                    } else {
                        if (!passwordO.equals(passwordT)) {
                            Toast.makeText(ZhuCe.this, "两次输入密码不统一", Toast.LENGTH_LONG).show();
                        } else if (passwordO.equals(passwordT)) {
                            // 将号码、验证码、密码等发送到服务器进行保存

                            handler = new Handler() {
                                @Override
                                public void handleMessage(Message msg) {
                                    super.handleMessage(msg);

                                    if (msg.what == 1) {

                                        Toast.makeText(ZhuCe.this, "恭喜，注册成功", Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(ZhuCe.this, "抱歉，注册失败", Toast.LENGTH_LONG).show();

                                    }
                                }
                            };

                            ZhuCeH(numberP, yanzhengM, passwordO);

                        }
                    }
                } else {

                }

            }
        });
    }

    //  发送短信获取验证码
    private void duanxin (final String userNumber) {
        new Thread() {
            @Override
            public void run() {
                super.run();

                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://api.zhaibao.cn/sendRegCode?mobile=" + userNumber + "&version=1.0&deviceType=2" );

                try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    //200请求成功
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    } else {
                        Log.d("错误", "验证码发送失败");
                    }

                } catch (IOException e) {
                    Log.d("错误:", e.toString());
                }
            }
        }.start();
    }

    //  完成注册
    private void ZhuCeH (final String usernumber, final String yanzhengma, final String userpassword) {

        new Thread() {
            @Override
            public void run() {
                super.run();
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://api.zhaibao.cn/sendRegCode?mobile=" + usernumber + "&code=" + yanzhengma + "&password" + userpassword + "&version=1.0&deviceType=2" );

                try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);

                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    } else {
                        //  进行其他的提示操作
                    }

                } catch (IOException e) {
                    Log.d("错误:", e.toString());
                }
            }
        }.start();

    }
}

